<?php
/**
 * User: tridungpham
 * Date: 7/6/11
 * Time: 11:08 AM
 */
 
class TemplateModel extends Model
{
    private $_id;
    
    function getActiveTemplate()
    {
        if($this->_id == null)
        {
            $this->loadTable('general');
            $rs = $this->tables['general']->get(array('field_name' => 'default_frontend_template'));
            if ($rs->getSize() == 0)
            {
                exit('No template found');
            }
            else
            {
                $row = $rs->getRow(0);
                $this->_id = $row->field_value;
                return $row->field_value;
            }
        }
        else
            return $this->_id;
    }

    function getTemplateConfig()
    {
        $id = $this->getActiveTemplate();
        $tpl =& $this->getTemplateDetail($id);
        $tplBlock =& $this->getTemplateBlocks($id);

        $arrayConfig = array();
        $arrayConfig['master'] = array($tpl->master_path,($tpl->is_general == 1)?'general':'component');
        $arrayConfig['default_render_block'] = $tpl->default_block;
        $arrayConfig['block'] = array();
        if(!is_null($tplBlock))
        {
            $size = $tplBlock->getSize();
            for($i = 0;$i < $size; $i++)
            {
                $row = $tplBlock->getRow($i);
                $arrayConfig['block'][$row->name] = array($row->path,($row->is_general == 1)?'general':'component');
            }
        }
        return $arrayConfig;
    }

    function getTemplateDetail($id)
    {
        $this->loadTable('templates');
        $rs = $this->tables['templates']->get(array('id' => $id));
        if($rs->getSize() == 0)
            exit('No template detail found');
        else
            return $rs->getRow(0);
    }

    function getTemplateBlocks($id)
    {
        $this->loadTable('templateBlocks');
        $rs = $this->tables['templateblocks']->get(array('template_id' => $id));
        if($rs->getSize() == 0)
            return NULL;
        else
            return $rs;
    }
}